package com.fc.test.controller.gen;

import com.fc.test.model.auto.Eps;
import com.fc.test.model.auto.ExamPaperAnswer;
import com.fc.test.model.auto.Question;
import com.fc.test.service.ExamPaperAnswerService;
import com.fc.test.service.QuestionService;
import com.fc.test.shiro.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.ExamPaperQuestionCustomerAnswer;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.ExamPaperQuestionCustomerAnswerService;
import io.swagger.annotations.Api;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("ExamPaperQuestionCustomerAnswerController")
@Api(value = "")
public class ExamPaperQuestionCustomerAnswerController extends BaseController{
	
	private String prefix = "gen/examPaperQuestionCustomerAnswer";
	@Autowired
	private ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
	@Autowired
	private ExamPaperAnswerService examPaperAnswerService;
	@Autowired
	private QuestionService questionService;
	@GetMapping("view")
	@RequiresPermissions("gen:examPaperQuestionCustomerAnswer:view")
    public String view(ModelMap model)
    {	
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	//@Log(title = "集合查询", action = "111")
	@PostMapping("list")
	@RequiresPermissions("gen:examPaperQuestionCustomerAnswer:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<ExamPaperQuestionCustomerAnswer> page=examPaperQuestionCustomerAnswerService.list(tablepar,searchTxt) ; 
		TableSplitResult<ExamPaperQuestionCustomerAnswer> result=new TableSplitResult<ExamPaperQuestionCustomerAnswer>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
     * 新增
     *//*

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }*/
	
	//@Log(title = "新增", action = "111")
	@PostMapping("add")
	@ResponseBody
	public AjaxResult add(Eps eps  ){
		List<ExamPaperQuestionCustomerAnswer> ep=eps.getEps();
		int b=0;
		ExamPaperAnswer examPaperAnswer=new ExamPaperAnswer();
        int s=0;
		for (ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer:ep
			 ) {
			examPaperAnswer.setPaperId(examPaperQuestionCustomerAnswer.getPaperId());
			examPaperQuestionCustomerAnswer.setDoRight(2);
			String answer=examPaperQuestionCustomerAnswer.getAnswer();
			Question question=questionService.selectByPrimaryKey(examPaperQuestionCustomerAnswer.getQuestionId());
			if(question.getCorrect().equals(answer)){
				s+=question.getScore();
				examPaperQuestionCustomerAnswer.setDoRight(1);
				examPaperQuestionCustomerAnswer.setCreateTime(new Date());
				examPaperQuestionCustomerAnswer.setUserScore(question.getScore());
			}

			b+=examPaperQuestionCustomerAnswerService.insertSelective(examPaperQuestionCustomerAnswer);
		}
		examPaperAnswer.setCreateUser(ShiroUtils.getUser().getUsername());
		examPaperAnswer.setCreateTime(new Date());
		examPaperAnswer.setStatus(1);
		examPaperAnswer.setUserScore(s);
		examPaperAnswerService.insertSelective(examPaperAnswer);
		if(b==ep.size()){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除", action = "111")
	@PostMapping("remove")
	@RequiresPermissions("gen:examPaperQuestionCustomerAnswer:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=examPaperQuestionCustomerAnswerService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer){
		int b=examPaperQuestionCustomerAnswerService.checkNameUnique(examPaperQuestionCustomerAnswer);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("ExamPaperQuestionCustomerAnswer", examPaperQuestionCustomerAnswerService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "修改", action = "111")
    @RequiresPermissions("gen:examPaperQuestionCustomerAnswer:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamPaperQuestionCustomerAnswer record)
    {
        return toAjax(examPaperQuestionCustomerAnswerService.updateByPrimaryKeySelective(record));
    }

    
    

	
}
